import axios from "../../utils/axios"

// pages/creatshop/creatshop.ts
const Multipart = require('../../utils/Multipart.min.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabbarlist:['基本信息','添加商品'],
    tabbarindex:0,
    fileList:[],
    shopname:"",
    shoptypes:["娱乐","丽人","生活"],
    typeindex:0,
    startpicker:false,
    endpicker:false,
    currenttime:new Date().getTime(),
    shopstart:"请选择时间",
    shopend:"请选择时间",
    onaddcondiction:false,
    conditions:[],
    sellerRequestDtoS:[{name:'',price:'',description:'',picture:[]}]
  },
  changetabbar(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      tabbarindex:index
    })
  },
  del(event){
    let id = event.detail.index   //能获取到对应的下标
    let fileList = this.data.fileList  //这里是前端页面展示的数组
    fileList.splice(id,1)    //根据下标来删除对应的图片
    this.setData({
      fileList:fileList,  //在这里进行重新赋值  删除后 图片剩几张就相当于给后台传几张
    })
 },
 getimage(e){
  console.log(e.detail)
  var fileList=this.data.fileList
  console.log(e.detail.file)
  var files=e.detail.file
  files.forEach(item => {
    item.url=item.path
    fileList.push(item)
  });
  this.setData({
    fileList:fileList
  })
},
changetypeindex(e){
  var index=e.currentTarget.dataset.index
  this.setData({
    typeindex:index
  })
},
getdatetime(e){
  var id=e.currentTarget.id
  var datetimestr=JSON.stringify(new Date(e.detail))
  console.log(datetimestr)
  var datetimearr=datetimestr.split("T")
  var datetimearr1=datetimearr[1].split(".")
  var datetime=(datetimearr[0]+" "+datetimearr1[0]).split('"')[1]
  this.setData({
    [id]:datetime
  })
  this.unshowpicker()
},
unshowpicker(){
  this.setData({
    startpicker:false,
    endpicker:false
  })
},
showpicker(e){
  var id=e.currentTarget.id
  this.setData({
    [id]:true
  })
},
datechage:function(e){
  console.log(666)
  this.setData({
    date:e.detail.value
  })
},
showaddcondition(){
  this.setData({
    onaddcondiction:true
  })
},
addcondition(e){
  var conditions=this.data.conditions
  conditions.push(e.detail.value)
  this.setData({
    conditions:conditions,
    onaddcondiction:false
  })
  console.log(this.data.conditions)
},
subcondition(e){
  var index=e.currentTarget.dataset.index
  var conditions=this.data.conditions
  conditions.splice(index,1)
  this.setData({
    conditions:conditions
  })
},
getgoodsdetail(e){
  var id=e.currentTarget.id
  var index=e.currentTarget.dataset.index
  var key="sellerRequestDtoS["+index+"]."+id
  this.setData({
    [key]:e.detail
  })
  console.log(this.data.sellerRequestDtoS)
},
getgoodsimage(e){
  console.log(e.detail)
  var index=e.currentTarget.dataset.index
  var fileList=this.data.sellerRequestDtoS[index].picture
  console.log(e.detail.file)
  var files=e.detail.file
  files.forEach(item => {
    item.url=item.path
    fileList.push(item)
  });
  var key="sellerRequestDtoS["+index+"].picture"
  this.setData({
  [key]:fileList
  })
},
delgoodsimage(e){
  let id = e.detail.index
  var index=e.currentTarget.dataset.index   //能获取到对应的下标
  let fileList = this.data.sellerRequestDtoS[index].picture  //这里是前端页面展示的数组
    fileList.splice(id,1)    //根据下标来删除对应的图片
    var key="sellerRequestDtoS["+index+"].picture"
    this.setData({
      [key]:fileList,  //在这里进行重新赋值  删除后 图片剩几张就相当于给后台传几张
    })
},
submit:function(e){
    var that=this
    var token=getApp().globalData.accessToken
    wx.showModal({
      title:"是否确认提交",
      success:async function(res){
        if(res.confirm){
        var fileList=that.data.fileList
        var files=[];
        console.log(fileList)  //构建的promise数组
           fileList.forEach((item,index)=>{  //item为图片的url
            var file={filePath:item.tempFilePath}
            files.push(file)
           })
        const field={
          type:that.data.shoptypes[that.data.typeindex],
          name:that.data.shopname,
          start:that.data.shopstart,
          end:that.data.shopend,
          conditions:that.data.conditions,
          location:that.data.location,
        }      
         var fields = Object.keys(field).map(key => ({
           name: key,
           value: field[key]
         }))
         files.map((item) => {
          return Object.assign(item, {
            name: "files",filePath:item.filePath })
        });
         var sellerRequestDtos=that.data.sellerRequestDtoS
         for (let i = 0; i < sellerRequestDtos.length; i++) {
           const item = sellerRequestDtos[i];
           fields.push({
             name:"sellerRequestDtoS["+i+"].name",
             value:sellerRequestDtos[i].name
           })
           fields.push({
             name:"sellerRequestDtoS["+i+"].price",
             value:sellerRequestDtos[i].price
           })
           fields.push({
            name:"sellerRequestDtoS["+i+"].description",
            value:sellerRequestDtos[i].description
          })
          console.log(sellerRequestDtos[i].picture)
          for (let j = 0; j < sellerRequestDtos[i].picture.length; j++) {
            const element = sellerRequestDtos[i].picture[j];
            files.push({
              filePath:sellerRequestDtos[i].picture[j].tempFilePath,
              name:"sellerRequestDtoS["+i+"].picture"
            })
          }
         }
        console.log(fields)   
        console.log(files)
        axios.upload('/shop',fields,files)
        wx.showModal({
          title:"已提交,将在3-5个工作日审核",
          success:res=>{
            if(res.confirm){
              wx.navigateBack({
                delta:1
              })
            }
          }
        })
        }
      }
    })

  },
  addgoods(){
  var goods=this.data.sellerRequestDtoS
  goods.push({name:'',price:'',description:'',picture:[]})
  this.setData({
    sellerRequestDtoS:goods
  })
  },
  delgoods(e){
    var index=e.currentTarget.dataset.index
    var goods=this.data.sellerRequestDtoS
    console.log(index)
    console.log(goods)
    goods.splice(index,1)
    this.setData({
      sellerRequestDtoS:goods
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})